import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Date;
import org.opengauss.copy.CopyManager;
import org.opengauss.core.BaseConnection;

/**
 * @作者： @wan005
 * @时间： 2023/3/3
 **/

public class jdbc_Certification_Case0006 {

    static public String driver = "org.opengauss.Driver";
    public static Properties getConfigFromFile(String filePath) {
        Properties props = new Properties();
        try {
            BufferedInputStream config = new BufferedInputStream(new FileInputStream(filePath));
            props.load(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return props;
    }

    public static String genURLFromPro(Properties props) {
        String hostname = props.getProperty("hostname");
        String[] hostnames = hostname.split(",");
        String port = props.getProperty("port");
        String[] ports = port.split(",");
        String dbname = props.getProperty("dbname");
        return genURLFromHostsPortsDBname(hostnames, ports, dbname);
    }

    public static String genURLFromHostsPortsDBname(String[] hostnames, String[] ports, String dbname) {
        String sourceURL;
        if (hostnames.length == 1) {
            sourceURL = "jdbc:opengauss://" + hostnames[0] + ":" + ports[0] + "/" + dbname;
        } else {
            ArrayList<String> ip_port_pare = new ArrayList<>();
            if (ports.length == 1) {
                for (String ip : hostnames) {
                    ip_port_pare.add(ip + ":" + ports[0]);
                }
            } else {
                for (int i = 0; i < hostnames.length; i++) {
                    ip_port_pare.add(hostnames[i] + ":" + ports[i]);
                }
            }
            sourceURL = "jdbc:opengauss://" + String.join(",", ip_port_pare) + "/" + dbname;
        }
        return sourceURL;
    }

    public static Connection GetConnection(Properties props) {
        Connection connR;
        String sourceURL = genURLFromPro(props);
        System.out.println(sourceURL);
        try {
            Class.forName(driver).newInstance();
            connR = DriverManager.getConnection(sourceURL, props);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
        return connR;
    }

    public static void main(String[] args) {
        Properties pros = new Properties();
        Properties pros_conf=null;
        String jdbc_config_file = null;
        String table = "t_certify_0006";
        String savepoint = "svp_certify_0006";
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "--config-file":
                    jdbc_config_file = args[i + 1];
                    pros_conf = getConfigFromFile(jdbc_config_file);
                    break;
                case "-F":
                    jdbc_config_file = args[i + 1];
                    pros_conf = getConfigFromFile(jdbc_config_file);
                    break;
                default:
                    break;
            }
        }
        if (pros_conf != null){
            pros_conf.putAll(pros);
        }
        Connection conn = GetConnection(pros_conf);
        System.out.println(conn);
        if(conn == null){
            System.out.println("连接失败");
            return;
        }else{
            try{
                System.out.println("-----savepoint测试-----");
                String sql6_01 = "drop table if exists " + table + ";" +
                    "create table " + table + "(a int);";
                String[] sql6_02 = {"insert into " + table + " values(2);",
                    "savepoint " + savepoint + ";",
                    "insert into " + table + " values(3);",
                    "release savepoint " + savepoint + ";"};
                String sql6_03 = "select * from " + table + ";";
                PreparedStatement ps_create = conn.prepareStatement(sql6_01);
                ps_create.execute();
                ps_create.close();

                PreparedStatement preparestmt = null;
                conn.setAutoCommit(false);
                for (int i = 0; i < sql6_02.length; i++) {
                    preparestmt = conn.prepareStatement(sql6_02[i]);
                    boolean hasResultSet = preparestmt.execute();
                    if (hasResultSet) {
                        ResultSet rs = preparestmt.getResultSet();
                        while (rs.next()){
                            System.out.println("6_02.第" + i + "行结果:" + 
                                rs.getString(1));
                        }
                    } else {
                        int count = preparestmt.getUpdateCount();
                        System.out.println("6_02.第" + i + "行更新:" + count);
                    }
                    preparestmt.close();
                }
                conn.commit();
                conn.setAutoCommit(true);
                PreparedStatement ps_select = conn.prepareStatement(sql6_03);
                boolean hasResultSet = ps_select.execute();
                if (hasResultSet) {
                    ResultSet rs = ps_select.getResultSet();
                    while (rs.next()){
                        System.out.println("6_03.结果:" + rs.getString(1));
                    }
                } else {
                    int count = ps_select.getUpdateCount();
                    System.out.println("6_03.更新行:" + count);
                }
                ps_select.close();

                String sqldel = "drop table if exists " + table + ";";
                Statement stmt=conn.createStatement();
                boolean hasResultSetdrop = stmt.execute(sqldel);
                if (hasResultSetdrop) {
                    ResultSet rs = stmt.getResultSet();
                    while (rs.next()){
                        System.out.println("6_drop.结果:" + rs.getString(1));
                    }
                } else {
                    int count = stmt.getUpdateCount();
                    System.out.println("6_drop.更新行:" + count);
                }
                stmt.close();
            }catch (SQLException s){
                s.printStackTrace();
            }
        }
    }
}